﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Alos.Common;
using Alos.Entity;
using Alos.IService;
using System.Web;
using System.Threading.Tasks;
using System.Text;
using Alos.Entity.Dto;

namespace Alos.LanClass.Api
{
    /// <summary>
    /// 教师上课记录表
    /// </summary>
    public class ReTeachRecordController : ApiController
    {
        
        IReTeachRecordService ReTeachRecordService;
        IGroupInfoService GroupInfoService;
        ISitPosSettingService SitPosSettingService;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="_IReTeachRecordService"></param>
        public ReTeachRecordController(IReTeachRecordService _IReTeachRecordService, IGroupInfoService _GroupInfoService, ISitPosSettingService _SitPosSettingService)
        {
            ReTeachRecordService = _IReTeachRecordService;
            GroupInfoService = _GroupInfoService;
            SitPosSettingService = _SitPosSettingService;
        }
        /// <summary>
        /// （开始上课）添加教师上课记录（如果上1堂课未结束 先结束上1堂课在重新添加）
        /// <param name="data"> data.reTeachRecord(教师上课记录) </param>
        /// </summary>
        [HttpPost]
        [Route("api/ReTeachRecord/StartReTeachRecord")]
        public JsonRtn<dynamic> StartReTeachRecord([FromBody]ReTeachRecord reTeachRecord)
        {
            var jsRtn = new JsonRtn<dynamic>();
            ////座位调整
            //SitPosSetting SitPosSetting = data.SitPosSetting;
            //if (SitPosSetting.ID>0)
            //{                
            //    jsRtn.message += SitPosSettingService.UpdateSitPosSetting(SitPosSetting.TeacherId, SitPosSetting.ClassId, SitPosSetting.ColNum, SitPosSetting.SequStduIds) ? "座位调整成功" : "座位调整失败";
            //}
            
            ////班级分组调整
            //GroupInfoService.DeleteGroupInfoByTeachaerID(SitPosSetting.TeacherId, SitPosSetting.ClassId);
            
            //    foreach (GroupInfo GroupInfo in data.GroupInfos)
            //    {
            //        jsRtn.message += GroupInfoService.CreatGroupInfo(GroupInfo) ? "班级分组成功" : GroupInfo.GroupName + "班级分组失败";
            //    }
            
            
            //上课表新增
            //ReTeachRecord reteach=ReTeachRecordService.getReTeachRecord((int)Common.Current.User.id, reTeachRecord.ClassID);
            //if (reteach.ID>0)
            //{
            //    jsRtn.message += ReTeachRecordService.UpdateReTeachRecord(reteach.ID) > 0 ? "上1堂课成功结束" : "上1堂课结束失败";  
            //}
            jsRtn.message += ReTeachRecordService.CreateReTeachRecord(reTeachRecord) > 0 ? "开始上课成功" : "开始上课失败";
            jsRtn.data = DateTime.Now.Ticks;
            return jsRtn;           
        }
        /// <summary>
        /// 检查上一堂课是否正常结束
        /// <param name="ClassID">班级ID</param>
        /// </summary>
        [HttpGet]
        [Route("api/ReTeachRecord/getReTeachRecord")]
        public  JsonRtn<dynamic> getReTeachRecord(int ClassID)
        {
            var jsRtn = new JsonRtn<dynamic>();

            jsRtn.data = ReTeachRecordService.getReTeachRecord(Common.Current.User.id, ClassID);
            return jsRtn;
        }

        /// <summary>
        /// 检查学生登录后是等待老师上课还是正在上课
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("api/ReTeachRecord/IsBeginTeach")]
        public JsonRtn<dynamic> IsBeginTeach()
        {
            var sui = ReTeachRecordService.GetReTeachRecord();
            return new JsonRtn<dynamic>() { state = sui != null && sui.ID > 0 ? RtnState.sucess : RtnState.fail, data = new { data = sui}, message ="" };
        }
        /// <summary>
        /// 点击下课
        /// <param name="LessionID">备课ID</param>
        /// </summary>
        [HttpPost]
        [Route("api/ReTeachRecord/EndReTeachRecord")]
        public JsonRtn<dynamic> EndReTeachRecord([FromBody]int LessionID)
        {
            var jsRtn = new JsonRtn<dynamic>();
            try
            {
                ReTeachRecordService.EndReTeachRecord(Common.Current.User.id,LessionID);
                jsRtn.state = RtnState.sucess;
                jsRtn.message = "成功";
                
            }
            catch (Exception)
            {
                jsRtn.state = RtnState.error;
                jsRtn.message = "失败";
                throw;
            }
            return jsRtn;
        }
        /// <summary>
        /// 另外开始上课
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/ReTeachRecord/StartOtherClass")]
        public JsonRtn<dynamic> StartOtherClass([FromBody]StartOtherClassParam data) 
        {
            var jsRtn = new JsonRtn<dynamic>();
            //首先结束上一堂课
            try
            {
                ReTeachRecordService.EndPreTeachRecord(data.ID);
                ReTeachRecord record = new ReTeachRecord();
                record.ClassID = Convert.ToInt32(data.ClassID);
                record.LessionID = Convert.ToInt32(data.LessionID);
                record.TeacherID = Convert.ToInt32(data.TeacherID);
                jsRtn.message = ReTeachRecordService.CreateReTeachRecord(record) > 0 ? "开始上课成功" : "开始上课失败";
                
            }
            catch (Exception ex)
            {
                jsRtn.message = "重新开始上课失败。";
                jsRtn.state = RtnState.error;
                throw;
            }

            jsRtn.data = DateTime.Now.Ticks;
            return jsRtn;       
        }
    }
}
